var app = getApp();
var WxParse = require('../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xsyc:{},
    ycid:'',
    usid: '',
    startgray: '../../images/score_stars_icon_gray.png',//星级灰色
    startyellow: '../../images/score_stars_icon_yellow.png',//星级黄色
    starsy: 0,
    starsn: 5,
    pls:[],
    plspage: { //心得体会每页展示
      page: 1,
      limit: 10,
      totalpage: 0
    },
    plcontent:'',
    showModalStatus:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    this.setData({
      ycid: options.ycid,
      usid: usid
    })
    wx.showLoading({
      title: '加载中',
    })
    this.getxsycDetail();
    this.getxsycPl();
  },



  getxsycDetail(e) {
    var that = this;
    var ycid = that.data.ycid;
    app.wxget({
      url: 'wxsyc/getxsycDetail.html',
      data: {
        ycid: ycid
      },
      success: function (res) {
        var xsyc = res.data;
        that.setData({
          xsyc: xsyc
        });
        wx.setNavigationBarTitle({
          title: "《" + res.data.ZPMC + "》"
        })
        if (res.data.CONTENT) {
          WxParse.wxParse('cpnr', 'html', res.data.CONTENT, that, 5);
        } 
        wx.hideLoading()
      }
    })
  },



  getxsycPl(e) {
    var that = this;
    var ycid = that.data.ycid;
    var page = that.data.plspage.page;
    var limit = that.data.plspage.limit;
    app.wxget({
      url: 'wxsyc/getxsycPl.html',
      data: {
        ycid: ycid,
        page: page,
        limit: limit
      },
      success: function (res) {
        var pls = res.data.list;
        var opls = that.data.pls;
        for (var i = 0; i < pls.length; i++) {
          opls.push(pls[i]);
        }
        var totalpage = res.data.totalpage;
        that.setData({
          pls: opls,
          "plspage.totalpage": totalpage
        });
      }
    })
  },



  //预览心得体会图片
  viewimage: function (e) {
    var cuindex = e.target.dataset.index;
    var that = this;
    var xsyc = this.data.xsyc;
    var pics = xsyc.pics;
    var urls = [];
    for (var i = 0; i < pics.length; i++) {
      urls.push(pics[i].PICURL);
    }
    wx.previewImage({
      current: urls[cuindex], // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },



  tofzbpxg(e) {
    var ycid = this.data.ycid;
    wx.navigateTo({
      url: '../../pages/fbzpxg/fbzpxg?ycid=' + ycid
    })
  },


  moreplBybid(e) {
    var page = this.data.plspage.page;
    this.setData({
      "plspage.page": parseInt(page) + 1,
    })
    this.getxsycPl();
  },



  //打开阅读点评modal
  powerDrawer(e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu);
  },


  util(currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200, //动画时长  
      timingFunction: "linear", //线性  
      delay: 0 //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })
      //关闭  
      if (currentStatu == "close") {
        this.setData({
          showModalStatus: false
        });
      }
    }.bind(this), 200)
    // 显示  
    if (currentStatu == "open") {
      this.setData({
        showModalStatus: true
      });
    }
  },


  //点击灰色星星
  addstar(e) {
    var stars = e.currentTarget.dataset.stars;
    var starsy = this.data.starsy;//黄色的星星
    var starsn = this.data.starsn;//灰色的星星
    var yellow = parseInt(starsy) + parseInt(stars) + 1;
    var grew = 5 - parseInt(yellow);
    this.setData({
      starsy: yellow,
      starsn: grew
    })
  },

  //点击黄色星星
  substar(e) {
    var stars = e.currentTarget.dataset.stars;
    var yellow = stars;
    var grew = 5 - parseInt(stars);
    this.setData({
      starsy: yellow,
      starsn: grew
    })
  },


  //获取评论
  getplcontent(e) {
    this.setData({
      plcontent: e.detail.value
    })
  },

  savebookxsycpl(e) {
    var that=this;
    var ycid = this.data.ycid;
    var usid = this.data.usid;
    var sname = wx.getStorageSync('name');
    var nj = wx.getStorageSync('nj');
    var bj = wx.getStorageSync('bj');
    var plcontent = this.data.plcontent;
    var pstar = this.data.starsy;
    if (plcontent==""){
      wx.showModal({
        title: '友情提示',
        content: '请输入评论内容!',
        showCancel: !1,
      })
      return;
    }
    wx.showLoading({
      title: '保存中',
    })
    app.wxpost({
      url: 'wxsyc/addBookxsycpl.html',
      data: {
        ycid: ycid,
        usid: usid,
        sname: sname,
        nj: nj,
        bj: bj,
        plcontent: plcontent,
        pstar: pstar
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.result) {
          wx.showModal({
            title: '成功',
            content: res.data.msg,
            showCancel: !1,
          });
          that.util("close");
          that.setData({
            plspage: {
              page: 1,
              limit: 10,
              totalpage: 0
            },
            pls:[],
          })
          that.getxsycPl();
        } else {
          wx.showModal({
            title: '失败',
            content: res.data.msg,
            showCancel: !1,
          });
        }
      }
    })
  },


  
  deleteXsyc(e) {
    var that = this;
    var ycid = that.data.ycid;
    app.wxget({
      url: 'wxsyc/deleteXsycByycid.html',
      data: {
        ycid: ycid
      },
      success: function (res) {
        if (res.data.result) {
          wx.showModal({
            title: '成功',
            content: res.data.msg,
            showCancel: !1,
          });
          wx.navigateTo({
            url: '../../pages/xsyc/xsyc'
          })
        } else {
          wx.showModal({
            title: '失败',
            content: res.data.msg,
            showCancel: !1,
          });
        }
      }
    })
  },

})